package com.ucar.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

import com.ucar.R;

public abstract class BaseDialog extends Dialog {

	protected final Context context;

	public BaseDialog(Context context) {
		super(context, R.style.BaseDialog);
		this.context = context;
	}

	protected BaseDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		this.context = context;
	}

	public BaseDialog(Context context, int theme) {
		super(context, R.style.BaseDialog);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init(context);
	}

	protected void init(Context context) {
		View view = getLayoutInflater().inflate(getLayoutId(), null);
		setContentView(view);
		getWindow().setGravity(Gravity.CENTER);
		setCanceledOnTouchOutside(true);
		setCancelable(true);
		initDialogViews();
		afterDialogViews();

		// 设置对话框透明度和宽高
		DisplayMetrics displayMetrics = getContext().getResources()
				.getDisplayMetrics();
		Window window = getWindow();
		LayoutParams attributes = window.getAttributes();
		attributes.width = (int) (displayMetrics.widthPixels);
		attributes.gravity = Gravity.BOTTOM;
		attributes.alpha = 0.9f;
		window.setAttributes(attributes);
		window.setWindowAnimations(R.style.BottomDialogWindowAnimation);  //添加动画  
	}

	protected abstract void afterDialogViews();

	protected abstract int getLayoutId();

	protected abstract void initDialogViews();
}
